package com.rachel.studyapp.activity;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.rachel.studyapp.R;
import com.rachel.studyapp.entries.UserInfo;
import com.rachel.studyapp.mvp.ContractUtils;
import com.rachel.studyapp.mvp.present.MinePresent;
import com.rachel.studyapp.utils.SPUtils;
import com.rachel.studyapp.utils.ToolUtils;

import de.greenrobot.event.EventBus;
import de.greenrobot.event.Subscribe;
import de.greenrobot.event.ThreadMode;

public class LoginActivity extends BaseActivity implements View.OnClickListener,ContractUtils.IMineView {
    private static final String TAG = "zsr";
    /**
     * ui
     */
    private ImageView mLeftIcon;
    private TextView mRightText;
    private TextView mMidText;
    private EditText mLoginUsername;
    private EditText mLoginPassword;
    private Button mLoginLogin;
    private LinearLayout mActivityLogin;
    private MinePresent mPresent;
    private ProgressDialog mProgressDialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_login);

        initView();
        mPresent = new MinePresent(this,this);
        EventBus.getDefault().register(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
    @Subscribe(threadMode = ThreadMode.MainThread)
    public void getRegisterInfo(UserInfo info){
        mLoginUsername.setText(info.getUsername());
        mLoginPassword.setText(info.getPassword());
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_login;
    }

    private void initView() {
        mLeftIcon = (ImageView) findViewById(R.id.left_icon);
        mRightText = (TextView) findViewById(R.id.right_text);
        mMidText = (TextView) findViewById(R.id.toolbar_text);
        mRightText.setOnClickListener(this);
        mLoginUsername = (EditText) findViewById(R.id.login_username);
        mLoginPassword = (EditText) findViewById(R.id.login_password);
        mLoginLogin = (Button) findViewById(R.id.login_login);
        mLoginLogin.setOnClickListener(this);
        mActivityLogin = (LinearLayout) findViewById(R.id.activity_login);

        mLeftIcon.setVisibility(View.VISIBLE);
        mLeftIcon.setImageResource(R.mipmap.back);
        mLeftIcon.setOnClickListener(this);
        mMidText.setVisibility(View.VISIBLE);
        mMidText.setText("用户登陆");
        mRightText.setVisibility(View.VISIBLE);
        mRightText.setText("注册");

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.right_text:
                startActivity(new Intent(this,RegisterActivity.class));
                break;
            case R.id.login_login:
                UserInfo info = new UserInfo();
                info.setUsername(mLoginUsername.getText().toString());
                info.setPassword(mLoginPassword.getText().toString());
                mPresent.login(info);
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(getWindow().peekDecorView().getWindowToken(),0);
                break;
            case R.id.left_icon:
                finish();
                break;
        }
    }


    @Override
    public void loginSuccess(UserInfo info) {
        Toast.makeText(this, info.getUsername()+"登陆成功", Toast.LENGTH_SHORT).show();
        EventBus.getDefault().post(info.getUsername());
        SPUtils.saveStringData(ToolUtils.SPNAME,ToolUtils.USERNAME,info.getUsername());
        finish();
    }

    @Override
    public void loginFailure(Object errorObj) {
        Toast.makeText(this, errorObj.toString(), Toast.LENGTH_SHORT).show();
    }

    @Override
    public void registerSuccess(UserInfo info) {

    }

    @Override
    public void registerFailure(Object errorObj) {

    }

    @Override
    public void showloading() {
        mProgressDialog = ProgressDialog.show(LoginActivity.this,null,"正在登陆，请稍后");
        mProgressDialog.setCancelable(true);
    }

    @Override
    public void hideloading() {
        mProgressDialog.dismiss();
    }

    @Override
    public void onFailure(Object errorObj) {

    }
}
